package com.nullpointer.xchat.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;

import com.androidquery.AQuery;
import com.androidquery.callback.ImageOptions;
import com.nullpointer.xchat.ProcessImage;
import com.nullpointer.xchat.R;
import com.nullpointer.xchat.fragment.ChooseImageDialogFragment.OnChooseImageDialogFragment;
import com.nullpointer.xchat.service.ChatService;
import com.nullpointer.xchat.utils.Common;
import com.nullpointer.xchat.utils.DataManager;
import com.nullpointer.xchat.utils.KiraGameLog;
import com.nullpointer.xchat.utils.Utils;

public class SayHiDialogFragment extends DialogFragment {
	private ImageView ivAvatar;
	private ImageButton ibEditAvatar;
	private EditText etStatus;
	private Button btPost;
	private Button btCancel;
	private String newAvatarUrl = null;
	private OnSayHiDialogFragment listener;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_sayhi, container);
		findViewIds(view);
		init();
		setEvent();
		return view;
	}

	private void init() {
		getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
		etStatus.setText(DataManager.getInstance(getActivity()).getUserStatus());
		String url = DataManager.getInstance(getActivity()).getUserAvatar();
		if (url != null) {
			showAvatar(url);
		}
	}

	private void setEvent() {
		ibEditAvatar.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				showChooseImageFragment();
			}
		});
		btPost.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Utils.hideKeyboard(getActivity(), etStatus);
				updateProfile();
			}
		});
		btCancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				if (listener != null) {
					listener.onCancel();
				}
				dismiss();
			}
		});
	}

	protected void updateProfile() {
		String status = etStatus.getText().toString();
		if (!status.equals(DataManager.getInstance(getActivity())
				.getUserStatus())
				|| (newAvatarUrl != null && !newAvatarUrl.equals(DataManager
						.getInstance(getActivity()).getUserAvatar()))) {
			ChatService.getInstance().updateProfile(null, status, newAvatarUrl);
			dismiss();
		} else {
			if (listener != null) {
				listener.onCancel();
			}
			dismiss();
		}

	}

	private void showAvatar(String url) {
		ImageOptions options = new ImageOptions();
		options.round = 5;
		options.fileCache = true;
		options.memCache = true;
		options.targetWidth = 96;
		// options.fallback =
		AQuery aq = new AQuery(getActivity());
		aq.id(ivAvatar).image(url, options);
	}

	protected void showChooseImageFragment() {
		final ChooseImageDialogFragment dialog = new ChooseImageDialogFragment();
		dialog.setOnChooseImageDialogFragment(new OnChooseImageDialogFragment() {

			@Override
			public void onGallery() {
				selectImageFromGallery();
			}

			@Override
			public void onCamera() {
				selectImageFromCamera();
			}
		});
		FragmentTransaction transaction = getActivity()
				.getSupportFragmentManager().beginTransaction();
		transaction.add(dialog, "ChooseImageDialogFragmentSayHi");
		transaction.commitAllowingStateLoss();
	}

	protected void selectImageFromGallery() {
		Intent intent = new Intent(getActivity(), ProcessImage.class);
		intent.putExtra(ProcessImage.IS_SELECT_IMAGE_FROM_CAMERA, false);
		intent.putExtra(ProcessImage.ASPECT_RATIO_X, 100);
		intent.putExtra(ProcessImage.ASPECT_RATIO_Y, 100);
		startActivityForResult(intent, Common.RequestCode.EDIT_AVATAR_SAY_HI);
	}

	protected void selectImageFromCamera() {
		Intent intent = new Intent(getActivity(), ProcessImage.class);
		intent.putExtra(ProcessImage.IS_SELECT_IMAGE_FROM_CAMERA, true);
		intent.putExtra(ProcessImage.ASPECT_RATIO_X, 100);
		intent.putExtra(ProcessImage.ASPECT_RATIO_Y, 100);
		startActivityForResult(intent, Common.RequestCode.EDIT_AVATAR_SAY_HI);
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		KiraGameLog.d("call here with  code " + requestCode);
		getActivity();
		if (resultCode != Activity.RESULT_OK) {
			KiraGameLog.d("resultCode is:" + resultCode + " cancel request");
			return;
		}
		switch (requestCode) {
		case Common.RequestCode.EDIT_AVATAR_SAY_HI:
			String url = data.getStringExtra(ProcessImage.IMAGE_URL);
			String localURL = data.getStringExtra(ProcessImage.LOCAL_URL);
			if (url != null) {
				newAvatarUrl = url;
				showAvatar(localURL);
			}
			break;

		default:
			break;
		}
	}

	private void findViewIds(View view) {
		ivAvatar = (ImageView) view.findViewById(R.id.iv_avatar);
		ibEditAvatar = (ImageButton) view.findViewById(R.id.ib_edit_avatar);
		etStatus = (EditText) view.findViewById(R.id.et_status);
		btPost = (Button) view.findViewById(R.id.bt_post);
		btCancel = (Button) view.findViewById(R.id.bt_cancel);
	}

	public void setOnSayHiDialogFragment(OnSayHiDialogFragment listener) {
		this.listener = listener;
	}

	public interface OnSayHiDialogFragment {
		void onCancel();
	}
}
